package com.example.basefream.service;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.Future;

@Service
@Slf4j
public class InasyncService {



    public static Random random =new Random();
    /*
    * 无参数返回
    * */
    @Async
    public void StringGetNum(int i){
//        try {
//            TimeUnit.SECONDS.sleep(1);
//            log.error("线程输出序号："+i);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        long beginTime=System.currentTimeMillis();
        int num=0;
        for (int j = 0; j < 100; j++) {
            num+=j;
        }
        System.out.println(System.currentTimeMillis());
    }
    /*
    * 有参数返回
    * */
    @Async("cusTomAsync")
    public Future<String> doTaskOne() throws Exception {
        System.out.println("开始做任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务一，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务一完成");
    }

    @Async("cusTomAsync")
    public Future<String> doTaskTwo() throws Exception {
        System.out.println("开始做任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务二，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务二完成");
    }

    @Async("cusTomAsync")
    public Future<String> doTaskThree() throws Exception {
        System.out.println("开始做任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务三，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务三完成");
    }

    @Async("cusTomAsync")
    public Future<String> doTaskFour() throws Exception {
        System.out.println("开始做任务四");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务四，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务四完成");
    }
    @Async("cusTomAsync")
    public Future<String> doTaskFive() throws Exception {
        System.out.println("开始做任务五");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务五，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务五完成");
    }
    @Async("cusTomAsync")
    public Future<String> doTaskSix() throws Exception {
        System.out.println("开始做任务六");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务六，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务六完成");
    }
    @Async("cusTomAsync")
    public Future<String> doTaskSeven() throws Exception {
        System.out.println("开始做任务七");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务七，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务七完成");
    }
    @Async("cusTomAsync")
    public Future<String> doTaskEight() throws Exception {
        System.out.println("开始做任务八");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务八，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务八完成");
    }
    @Async("cusTomAsync")
    public Future<String> doTaskNine() throws Exception {
        System.out.println("开始做任务九");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务九耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务九完成");
    }

    /*
    * 自定义无参数
    * */
    @Async("cusTomAsync")
    public void SelGetNum(){}

   /*
   * 自定义有参数
   * */
    @Async("cusTomAsync")
    public Future<JSONObject> getJson(){
        return new AsyncResult<>(new JSONObject());
    }
}
